home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 798 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.9 KB

  1. From: Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
  2. Message-ID: <31505061.2E03@cs.tu-berlin.de>
  3. X-Original-Date: Wed, 20 Mar 1996 19:37:21 +0100
  4. Path: in1.uu.net!bounce-back
  5. Date: 21 Mar 96 07:48:34 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Return-Path: <daemon@meeker.UCAR.EDU>
  8. Newsgroups: comp.std.c++
  9. Subject: Re: Constructors and conversion operator
  10. Organization: Technical University of Berlin
  11. References: <314E1D5D.76E5@cs.tu-berlin.de> <4imnnl$j29@engnews1.Eng.Sun.COM>
  12. X-Mailer: Mozilla 2.0 (Win95; I)
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBFAgUBMVEJ/uEDnX0m9pzZAQFNzgF/a32DUPua7gkyULne2dcBVFh31T4QDS2e
  15.     Pf8f1hNLPqxElrU1g8zUwxjga1EWr3SS
  16.     =wC2d
  17.  
  18. Steve Clamage wrote:
  19. > In article 76E5@cs.tu-berlin.de, Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
  20. > writes:
  21. > >
  22. > >The DWP defines:
  23. > >
  24. > >A constructor declared without the  function-specifier  explicit  that
  25. > >  can  be called with a single parameter specifies a conversion from the
  26. > >  type of its first parameter to the type of its  class. [class.conv.ctor]
  27. > >
  28. > >Now, to me this means that a copy constructor is a converting constructor
  29. > >since it takes a single parameter which is a reference to an object of the
  30. > >class ( I don't think that this is intended ).
  31. > I don't think it matters. The conversion is the identity conversion. In
  32. > places where conversions are required, there is almost always more than
  33. > one way to achieve the conversion. (Often there is an infinite number
  34. > of possible conversion sequences, such as short->int->short->int->
  35. > short->...->int.) The explicit rule in such cases is to choose the
  36. > shortest possible sequence of conversions.
  37. >
  38. > Suppose a copy constructor T::T(const T&) is viewed as a conversion
  39. > from T to T. If such a conversion is "needed", the shortest conversion
  40. > sequence must be chosen. That would be the null sequence, and so the
  41. > copy ctor would never be used or even considered for that purpose.
  42. > (Just as the sequence int->Rational->float would never be considered in
  43. > converting an int to a float.)
  44.  
  45. I'm not too sure here. First, if the copy constructor is the identity 
  46. conversion, why is it called at all? I mean, if the identity conversion is 
  47. eliminated from the conversion sequence, why isn't the call to the copy 
  48. constructor itself eliminated? Second, if the copy constructor is called, why 
  49. is it called only once? Consider
  50.  
  51. class A { A(const A&); };
  52. void foo(A);
  53. A a;
  54.  
  55. Now, foo(a) is implicitly converted to foo(A(a)). Why not to foo(A(A(a))? 
  56. Clearly, if the copy constructor is a conversion which has to be performed on 
  57. a, it *is* performed and it is performed only once, since no conversion is 
  58. needed thereafter. If the copy constructor is not a conversion, things get 
  59. complicated. 
  60. Actually, I can't agree that the copy constructor is the identity conversion. 
  61. It is a conversion from a reference to the referenced type, i.e. from A& to A 
  62. in this case. A& and A are two different types and the DWP doesn't say that a 
  63. conversion from A& to A (i.e. the copy constructor) isn't a conversion or is 
  64. to be eliminated from the conversion sequence or whatever... It just isn't 
  65. stated explicitly, probably because everybody knows it. I think this is the 
  66. problem.
  67.  
  68. Bye
  69.  
  70. Roman
  71. ---
  72. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  73. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  74. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  75. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  76. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  77. ---
  78. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  79. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  80. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  81. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  82. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  83.